package com.nirvana.item;

import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.awt.RelativePoint;
import com.nirvana.config.PluginConfig;
import com.nirvana.data.DefPropertyData;
import com.nirvana.data.DependenceData;
import com.nirvana.type.ItemType;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DefPropertyItem extends AbstractItem {
    private JTextField sourceNameTextField = new JTextField();
    private JTextField commentTextField = new JTextField();
    private final JComboBox<String> propertyTypeComboBox = new JComboBox<>();
    private final JComboBox<String> propertyResolveComboBox = new JComboBox<>();

    @Override
    public void init() {
        super.init();

        setComboBoxModel();
        propertyTypeComboBox.addItemListener(this::propertyChange);
        propertyResolveComboBox.addItemListener(this::propertyChange);
        propertyResolveComboBox.addMouseListener(new MouseAdapter() {
            Balloon balloon;
            @Override
            public void mouseEntered(MouseEvent e) {
                balloon = JBPopupFactory.getInstance()
                        .createHtmlTextBalloonBuilder(getTooltip(), MessageType.INFO, null)
                        .setHideOnKeyOutside(true)
                        .setHideOnLinkClick(true)
                        .setAnimationCycle(150)
                        .createBalloon();
                RelativePoint showPoint = new RelativePoint(e);
                balloon.show(showPoint, Balloon.Position.atLeft);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                balloon.hide();
            }
        });

        int column = 2;
        itemPanel.add(sourceNameTextField, getConstraintsStr(column++));
        itemPanel.add(commentTextField, getConstraintsStr(column++));
        itemPanel.add(propertyTypeComboBox, getConstraintsStr(column++));
        itemPanel.add(propertyResolveComboBox, getConstraintsStr(column++));
    }

    private String getTooltip() {
        DependenceData dependenceDataByType = PluginConfig.getInstance().getDependenceDataByType(getPropertyType());
        if (dependenceDataByType == null) return "";
        return dependenceDataByType.tooltip(getResolveMethodName());
    }

    private void setComboBoxModel() {
        propertyTypeComboBox.setModel(new DefaultComboBoxModel<>(PluginConfig.getInstance().types()));

        String propertyType = getPropertyType();
        propertyResolveComboBox.setModel(new DefaultComboBoxModel<>(PluginConfig.getInstance().resolves(propertyType)));
    }

    private void propertyChange(ItemEvent e) {
        if (e.getStateChange() != ItemEvent.SELECTED) return;

        Object source = e.getSource();
        if (source == propertyTypeComboBox) {
            propertyResolveComboBox.setModel(new DefaultComboBoxModel<>(PluginConfig.getInstance().resolves(getPropertyType())));
            propertyResolveComboBox.setSelectedIndex(0);
        }
    }

    @Override
    protected ItemType getItemType() {
        return ItemType.DefPropertyItem;
    }

    @Override
    public void copy(AbstractItem copyItem) {
        if (copyItem == null) return;
        if (copyItem instanceof DefPropertyItem defPropertyItem) {
            propertyTypeComboBox.setSelectedIndex(defPropertyItem.propertyTypeComboBox.getSelectedIndex());
            propertyResolveComboBox.setSelectedIndex(defPropertyItem.propertyResolveComboBox.getSelectedIndex());
        }
    }

    public void populate(DefPropertyData data) {
        propertyNameTextField.setText(data.getPropertyName());
        sourceNameTextField.setText(data.getSourceName());
        commentTextField.setText(data.getComment());
        propertyTypeComboBox.setSelectedIndex(data.getTypeIndex());
        propertyResolveComboBox.setSelectedIndex(data.getResolveIndex());
    }

    public String getOriginPropertyName() {
        return sourceNameTextField.getText();
    }

    public String getResolveMethodName() {
        return propertyResolveComboBox.getSelectedItem().toString();
    }

    public boolean isResolve() {
        return !"auto".equals(getResolveMethodName());
    }

    public String getComment() {
        return commentTextField.getText();
    }

    public String getPropertyType() {
        return propertyTypeComboBox.getSelectedItem().toString();
    }

    public void refreshComboBox() {
        String propertyType = getPropertyType();
        int typeIndex = PluginConfig.getInstance().getTypeIndex(propertyType);
        String resolveMethodName = getResolveMethodName();
        int resolveIndex = PluginConfig.getInstance().getResolveIndex(propertyType, resolveMethodName);
        setComboBoxModel();
        propertyTypeComboBox.setSelectedIndex(typeIndex);
        propertyResolveComboBox.setSelectedIndex(resolveIndex);
    }
}
